package com.bc004346.travelus.fragment;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.bc004346.travelus.R;
import com.bc004346.travelus.adapter.StandbyAdapter;
import com.bc004346.travelus.model.domain.Passenger;

import java.util.ArrayList;
import java.util.Collections;

import static com.bc004346.travelus.utility.Utility.log;

/**
 * Created by Renats on 10/27/2014.
 *
 */
public class StandbyFragment extends ListFragment {

    private static final String KEY_PAX = "com.bc004346.travelus.StandbyFragment.Pax";
    protected SwipeRefreshLayout mSwipeRefresh;
    private ArrayList<Passenger> mPax = new ArrayList<Passenger>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        if (savedInstanceState != null)
            //noinspection unchecked
            mPax = (ArrayList<Passenger>) savedInstanceState.getSerializable(KEY_PAX);
        mSwipeRefresh = (SwipeRefreshLayout) getActivity().findViewById(R.id.activity_fragment_swipeRefresh);

        StandbyAdapter adapter = new StandbyAdapter(getActivity(), mPax);
        setListAdapter(adapter);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        try {
            outState.putSerializable(KEY_PAX, mPax);
        } catch (Exception e) {
            log(e, getActivity());
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.item_selector, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        try {
            getListView().setOnScrollListener(new AbsListView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(AbsListView absListView, int i) {
                }

                @Override
                public void onScroll(AbsListView absListView,
                                     int firstVisibleItem,
                                     int visibleItemCount,
                                     int totalItemCount) {
                    if(mSwipeRefresh == null) return;

                    if (firstVisibleItem == 0) mSwipeRefresh.setEnabled(true);
                    else mSwipeRefresh.setEnabled(false);
                }
            });
        } catch (Exception e) {
            log(e, getActivity());
        }
    }

    public void refresh(ArrayList<Passenger> passengers){

        try {
            mPax.clear();
            final ListView lv = getListView();
            mPax.addAll(passengers);
            if (passengers.size() > 0) {

                Collections.sort(mPax);

                StandbyAdapter adapter = new StandbyAdapter(getActivity(), mPax);
                setListAdapter(adapter);

                if (lv != null) {
                    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
                    lv.setSelector(android.R.color.darker_gray);
                }

            } else {
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                        R.layout.list_flight_simple, new ArrayList<String>() {{
                    add("No standby passengers found");
                }}
                );
                setListAdapter(adapter);
                if (lv != null)
                    lv.setChoiceMode(ListView.CHOICE_MODE_NONE);

            }
        } catch (Exception e) {
            log(e, getActivity());
        }
    }
}
